package wef.PW.module;

import wef.PW.module.analysis.RewardSplit;
import wef.PW.module.analysis.RewardSplitImpl;
import wef.PW.utils.StrUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 发送报文
 */
public class RewardReceive {

    private Socket socket;
    private InputStream inputStream;
    private OutputStream outputStream;
    private Thread T_Read;
    private boolean isRun = true;
    private RewardSplit rewardAnalysis;
    private boolean state = false;

    public void setState(boolean state) {
        this.state = state;
    }

    /**
     * 创建ObjectOutput
     *
     * @param socket
     */
    public RewardReceive(Socket socket) {
        this.socket = socket;
        this.rewardAnalysis = new RewardSplitImpl();
        try {
            this.inputStream = socket.getInputStream();
            ReadReward();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 接收报文
     */
    public void ReadReward() {

        this.T_Read = new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    while (isRun) {
                        byte[] bytes = new byte[255];
                        int len = 0;
                        len = inputStream.read(bytes, 0, 255);
                        byte[] buff = new byte[len];
                        //拷贝数组
                        System.arraycopy(bytes, 0, buff, 0, len);
                        String reward = StrUtils.byteArrayToHexStr(buff);
                        //调用报文分类方法
                        System.out.println("接收到的报文：  " + reward.trim());

                        rewardAnalysis.classify(reward.trim());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        T_Read.start();

        new  Thread(){
            @Override
            public void run() {
                super.run();
                while (true){

                    state = false;
                    try {
                        SendReward("680443000000");
                        sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                        if (state){
                            System.out.println("连接正常");
                        }else {

                            System.out.println("断开连接");
                            close();
                            System.exit(0);
                        }

                }
            }
        }.start();
    }
    /**
     * 关闭连接
     */
    public void close() {
        try {
            this.isRun = false;
            inputStream.close();
            outputStream.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 发送报文
     */
    public boolean SendReward(String reward) {
        try {
            outputStream = socket.getOutputStream();
            outputStream.write(StrUtils.hexStrToByteArray(reward));
            outputStream.flush();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

}
